const app = getApp()
const favoritesUtil = require('../../utils/favorites')

Page({
  data: {
    favoritesList: [],
    isEmpty: true,
    currentUser: null
  },

  onLoad() {
    this.loadFavorites()
  },

  onShow() {
    this.loadFavorites()
  },

  // 加载收藏列表
  loadFavorites() {
    const currentUser = app.globalData.userInfo
    
    if (!currentUser) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
      return
    }

    const favorites = favoritesUtil.getFavorites(currentUser.id)
    
    // 格式化收藏时间
    const formattedList = favorites.map(item => ({
      ...item,
      addTimeText: favoritesUtil.formatFavoriteTime(item.favoriteTime)
    }))
    
    this.setData({
      favoritesList: formattedList,
      isEmpty: formattedList.length === 0,
      currentUser: currentUser
    })
  },

  // 播放音乐
  playMusic(e) {
    const music = e.currentTarget.dataset.music
    
    // 设置当前播放歌曲和播放列表
    app.globalData.currentSong = music
    app.globalData.currentPlaylist = this.data.favoritesList
    app.globalData.currentSongIndex = this.data.favoritesList.findIndex(item => item.id === music.id)
    
    // 播放歌曲
    app.playSong(music)
    
    // 跳转到播放器页面
    wx.navigateTo({
      url: '/pages/player/player'
    })
  },

  // 切换收藏状态
  toggleFavorite(e) {
    e.stopPropagation()
    const song = e.currentTarget.dataset.song
    const currentUser = app.globalData.userInfo
    
    wx.showModal({
      title: '取消收藏',
      content: '确定要取消收藏这首歌曲吗？',
      success: (res) => {
        if (res.confirm) {
          // 使用工具函数移除收藏
          const result = favoritesUtil.removeFromFavorites(currentUser.id, song.id)
          
          if (result) {
            // 重新加载收藏列表
            this.loadFavorites()
            
            wx.showToast({
              title: '已取消收藏',
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: '取消收藏失败',
              icon: 'none'
            })
          }
        }
      }
    })
  },

  // 显示更多操作
  showMoreActions(e) {
    e.stopPropagation()
    const song = e.currentTarget.dataset.song
    
    wx.showActionSheet({
      itemList: ['播放', '添加到歌单', '分享', '取消收藏'],
      success: (res) => {
        switch(res.tapIndex) {
          case 0:
            this.playMusic({ currentTarget: { dataset: { music: song } } })
            break
          case 1:
            wx.showToast({ title: '功能开发中', icon: 'none' })
            break
          case 2:
            wx.showToast({ title: '功能开发中', icon: 'none' })
            break
          case 3:
            this.toggleFavorite({ currentTarget: { dataset: { song } }, stopPropagation: () => {} })
            break
        }
      }
    })
  },

  // 播放全部
  playAll() {
    if (this.data.favoritesList.length === 0) {
      wx.showToast({
        title: '收藏列表为空',
        icon: 'none'
      })
      return
    }

    const firstSong = this.data.favoritesList[0]
    
    // 设置播放列表
    app.globalData.currentPlaylist = this.data.favoritesList
    app.globalData.currentSongIndex = 0
    app.globalData.currentSong = firstSong
    
    // 播放第一首歌
    app.playSong(firstSong)
    
    wx.showToast({
      title: '开始播放收藏列表',
      icon: 'success'
    })
    
    // 跳转到播放器
    wx.navigateTo({
      url: '/pages/player/player'
    })
  },

  // 清空所有收藏
  clearAllFavorites() {
    const currentUser = app.globalData.userInfo
    
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有收藏吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          // 使用工具函数清空收藏列表
          const result = favoritesUtil.clearFavorites(currentUser.id)
          
          if (result) {
            // 更新页面数据
            this.setData({
              favoritesList: [],
              isEmpty: true
            })
            
            wx.showToast({
              title: '已清空收藏',
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: '清空失败',
              icon: 'none'
            })
          }
        }
      }
    })
  }
})